package dev.touchswipe.ScreenLock.settingsManager
{
	import dev.touchswipe.shared.Fonts;
	import dev.touchswipe.shared.components.Button;
	import dev.touchswipe.shared.components.Checkbox;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.net.SharedObject;
	import flash.net.registerClassAlias;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	
	public class SettingsManager extends NativeWindow
	{
		private var _windowContent:Sprite;
		private var _settings:Settings;
		public function SettingsManager()
		{
			var initOptions:NativeWindowInitOptions=new NativeWindowInitOptions();
			initOptions.maximizable=false;
			initOptions.resizable=false;
			initOptions.systemChrome=NativeWindowSystemChrome.STANDARD;
			initOptions.type=NativeWindowType.NORMAL;
			super(initOptions);
			activate();
			visible=true;
			title="Swipe To Unlock Settings";
			
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.stageWidth=320;
			stage.stageHeight=280;
			stage.align=StageAlign.TOP_LEFT;
			
			_settings=loadSettings();
			
			_windowContent=new Sprite();
			stage.addChild(_windowContent);
			buildGUI();
		}
		
		
		private var backgroundPreview:Bitmap;
		public function buildGUI():void{
			_windowContent.graphics.beginFill(0x333333);
			_windowContent.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			
			var dtf:TextFormat=new TextFormat(Fonts.SegoeUIL,14,0xffffff);
			
			var titleLabel:TextField=new TextField();
			titleLabel.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,28,0x33ccff);
			titleLabel.text="Slide to Unlock Settings";
			titleLabel.selectable=false;
			titleLabel.width=stage.stageWidth;
			titleLabel.height=50;
			titleLabel.x=10;
			titleLabel.embedFonts=true;
			_windowContent.addChild(titleLabel);
			
			var pinLabel1:TextField=new TextField();
			pinLabel1.visible=false;
			pinLabel1.defaultTextFormat=dtf;
			pinLabel1.text="Enter Pin:";
			pinLabel1.selectable=false;
			pinLabel1.width=70;
			pinLabel1.height=20;
			pinLabel1.x=150;
			pinLabel1.y=45;
			pinLabel1.embedFonts=true;
			_windowContent.addChild(pinLabel1);
			
			var pin1:TextField=new TextField();
			pin1.visible=false;
			pin1.defaultTextFormat=dtf;
			pin1.displayAsPassword=true;
			pin1.type=TextFieldType.INPUT;
			pin1.border=true;
			pin1.maxChars=4;
			pin1.width=40;
			pin1.height=20;
			pin1.x=215;
			pin1.y=45;
			pin1.restrict="0-9";
			pin1.background=true;
			pin1.backgroundColor=0x33ccff;
			pin1.embedFonts=true;
			_windowContent.addChild(pin1);
			
			var usePinLabel:TextField=new TextField();
			usePinLabel.defaultTextFormat=dtf;
			usePinLabel.text="Use Pin:";
			usePinLabel.selectable=false;
			usePinLabel.width=70;
			usePinLabel.height=30;
			usePinLabel.x=10;
			usePinLabel.y=45;
			usePinLabel.embedFonts=true;
			_windowContent.addChild(usePinLabel);
			
			var usePin:Checkbox=new Checkbox();
			usePin.checked=(_settings.pinHash.length>0)?true:false;
			usePin.x=130;
			usePin.y=50;
			usePin.addEventListener(Event.SELECT,function(event:Event):void{
				pin1.visible=usePin.checked;
				pinLabel1.visible=usePin.checked;
			});
			_windowContent.addChild(usePin);
			
			var clockLabel:TextField=new TextField();
			clockLabel.defaultTextFormat=dtf;
			clockLabel.text="Show Clock:";
			clockLabel.selectable=false;
			clockLabel.width=120;
			clockLabel.height=30;
			clockLabel.x=10;
			clockLabel.y=70;
			clockLabel.embedFonts=true;
			_windowContent.addChild(clockLabel);
			
			var showClock:Checkbox=new Checkbox();
			showClock.checked=_settings.showClock;
			showClock.x=130;
			showClock.y=75;
			showClock.addEventListener(Event.SELECT,function(event:Event):void{
				_settings.showClock=showClock.checked;
			});
			_windowContent.addChild(showClock);
			
			var backgroundLabel:TextField=new TextField();
			backgroundLabel.defaultTextFormat=dtf;
			backgroundLabel.text="Backgound Image:";
			backgroundLabel.selectable=false;
			backgroundLabel.width=120;
			backgroundLabel.height=30;
			backgroundLabel.x=10;
			backgroundLabel.y=95;
			backgroundLabel.embedFonts=true;
			_windowContent.addChild(backgroundLabel);
			
			var useBackground:Checkbox=new Checkbox();
			useBackground.checked=false;
			useBackground.x=130;
			useBackground.y=100;
			useBackground.addEventListener(Event.SELECT,function(event:Event):void{
				if(useBackground.checked){
					selectBackgroundImage();
				}
				else{
					_settings.background="";
					backgroundPreview.bitmapData=new BitmapData(1,1);
				}
			});
			_windowContent.addChild(useBackground);
			
			backgroundPreview=new Bitmap();
			backgroundPreview.x=20;
			backgroundPreview.y=130;
			_windowContent.addChild(backgroundPreview);
			if(_settings.background!=""){
				useBackground.checked=true;
				loadBackground(_settings.background);
			}
			
			var okBtn:Button=new Button("Ok");
			okBtn.x=220;
			okBtn.y=90;
			okBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				if(pin1.visible)
					_settings.setPin(pin1.text);
				else if(!usePin.checked)
					_settings.pinHash="";
				saveSettings();
				close();
			});
			_windowContent.addChild(okBtn);
			
			var cancelBtn:Button=new Button("Cancel");
			cancelBtn.x=220;
			cancelBtn.y=130;
			cancelBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				close();
			});
			_windowContent.addChild(cancelBtn);
		}
		
		private function selectBackgroundImage():void{
			var fb:File=File.documentsDirectory;
			fb.browseForOpen("Select Background Image...",[new FileFilter("Images","*.png;*.jpg;*.gif;*.bmp")]);
			fb.addEventListener(Event.SELECT,function(event:Event):void{
				_settings.background=fb.nativePath;
				loadBackground(fb.nativePath);
			});
		}
		
		private function loadBackground(path:String):void{
			var fs:FileStream=new FileStream();
			fs.open(new File(path),FileMode.READ);
			var loader:Loader=new Loader();
			var data:ByteArray=new ByteArray();
			fs.readBytes(data);
			loader.loadBytes(data);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(event:Event):void{
				backgroundPreview.bitmapData=Bitmap(loader.content).bitmapData;
				var scale:Number=180/Math.max(backgroundPreview.bitmapData.height,backgroundPreview.bitmapData.width)
				backgroundPreview.width=backgroundPreview.bitmapData.width*scale;
				backgroundPreview.height=backgroundPreview.bitmapData.height*scale;
				fs.close();
			});
		}
		
		private var _so:SharedObject;
		public function loadSettings():Settings{
			registerClassAlias("SettingsManager.Settings",Settings);
			_so=SharedObject.getLocal("settings","/");
			if(_so.data==null||_so.data.version!=Settings.VERSION){
				_so.clear();
				_so.data.version=Settings.VERSION;
				_so.data.settings=new Settings();
				_so.flush();
				return Settings(_so.data.settings);
			}
			else{
				return Settings(_so.data.settings);
			}
		}
		
		public function saveSettings():void{
			_so.data.version=Settings.VERSION;
			_so.data.settings=_settings;
			_so.flush();
		}
	}
}